//
//  WelcomeViewController.m
//  yunqiphone
//
//  Created by ikeyyang on 15/09/25.
//  Copyright (c) 2015年 ci123.com. All rights reserved.
//

#import "WelcomeViewController.h"

@interface WelcomeViewController ()

@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)UIPageControl *pageControl;
@property (nonatomic,retain)NSMutableArray *imageArray;
@property (nonatomic,retain)NSArray *positionArray;
@property (nonatomic,assign)NSInteger currentIndex;
@property (nonatomic,assign)CGFloat offsetX;
@property (nonatomic,assign)CGFloat offsetY;
@property (nonatomic,assign)BOOL isAnimating;

@end

@implementation WelcomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _currentIndex = -1;
    }
    return self;
}

- (void)loadView
{
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor = RGBCOLOR(240, 239, 244);
    self.view = view;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self _initView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)_initView
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT)];
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = NO;
    [self.view addSubview:_scrollView];
    
    [_scrollView setContentSize:CGSizeMake(_scrollView.width * 3, _scrollView.height)];
    float width = _scrollView.width;
    CGFloat inch = [UIUtils applicationSreenInch];
    NSString *picUrl=@"";
    float marginTop;
    
    for (int i = 1; i <= 3; i++) {
        UIImageView *back = [[UIImageView alloc] initWithFrame:CGRectMake((i - 1) * width , 0, APP_SCREEN_WIDTH, APP_SCREEN_HEIGHT)];
        back.userInteractionEnabled=YES;
        
        if(inch==4.7 || inch==4.0){
            if(inch==4.7){
                marginTop=APP_SCREEN_HEIGHT-96;
            }else{
                marginTop=APP_SCREEN_HEIGHT-90;
            }
            
            picUrl=[NSString stringWithFormat:@"welcome_750_%d",i];
        }else if(inch==5.5){
            marginTop=APP_SCREEN_HEIGHT-98;
            picUrl=[NSString stringWithFormat:@"welcome_1242_%d",i];
        }else{
            marginTop=APP_SCREEN_HEIGHT-90;
            picUrl=[NSString stringWithFormat:@"welcome_640_%d",i];
        }
        
        [back setImage:[UIImage imageNamed:picUrl]];
        [_scrollView addSubview:back];
        
        if(i==3){
            UIButton  *startButton = [UIButton new];
            [startButton setTitle:@"进入" forState:UIControlStateNormal];
            [startButton setTitleColor:RGBCOLOR(255, 255, 255) forState:UIControlStateNormal];
            startButton.layer.borderWidth = 1;
            startButton.layer.borderColor = RGBCOLOR(255, 255, 255).CGColor;
            
            [startButton addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
            if (inch==4.7 || inch==4.0) {
                startButton.frame = CGRectMake((APP_SCREEN_WIDTH - 125)/2, APP_SCREEN_HEIGHT - 132, 125, 34);
                startButton.titleLabel.font = [UIFont systemFontOfSize:18];
                startButton.layer.cornerRadius = 34 / 2;
            }else if(inch == 5.5){
                startButton.frame = CGRectMake((APP_SCREEN_WIDTH - 240)/2, APP_SCREEN_HEIGHT - 215, 240, 54);
                startButton.titleLabel.font = [UIFont systemFontOfSize:30];
                startButton.layer.cornerRadius = 54 / 2;
            }else{
                startButton.frame = CGRectMake((APP_SCREEN_WIDTH - 110)/2, APP_SCREEN_HEIGHT - 79, 110, 27);
                startButton.titleLabel.font = [UIFont systemFontOfSize:16];
                startButton.layer.cornerRadius = 27 / 2;
            }
            [back addSubview:startButton];
        }
    }
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, APP_SCREEN_HEIGHT - 54, APP_SCREEN_WIDTH, 36)];
    [_pageControl setNumberOfPages:3];
    [_pageControl setPageIndicatorTintColor:RGBCOLOR(201, 201, 201)];
    [self.view addSubview:_pageControl];
}

#pragma mark - Private Method

- (void)dismissViewController
{
    if (self.endEvent) {
        self.endEvent();
        self.endEvent = nil;
    }
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float offsetX = scrollView.contentOffset.x;
    if (offsetX > (scrollView.contentSize.width - scrollView.width + 40)) {
        [self dismissViewController];
    }
    return;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = scrollView.contentOffset.x / scrollView.width;
    
    if (page == _currentIndex) {
        return;
    }
    
    _pageControl.currentPage = page;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0){
    return NO;
}

// New Autorotation support.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0){
    return NO;
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end
